package com.qf.web;

import com.qf.pojo.Admin;
import com.qf.service.AdminService;
import com.qf.service.serviceimpl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * 管理员登录
 * */
@WebServlet("/adminService")
public class AdminLoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //获得前台传来的数据
        String adminname = request.getParameter("adminname");
        String adminpassword = request.getParameter("adminpassword");
        //通过业务层获取Admin
        AdminService as = new AdminServiceImpl();
        Admin admin = as.find(adminname, adminpassword);
        if(admin != null){
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
            request.getRequestDispatcher("/admin/result.jsp").forward(request,response);
        }else{
            request.setAttribute("message","账号或密码错误!");
            request.getRequestDispatcher("/admin/Login.jsp").forward(request,response);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
